package com.tarena.lbs.activity.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.param.ActivityAddParam;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.api.UserApi;
import com.tarena.lbs.user.api.pojo.dto.AdminDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityRepository activityRepository;

    @DubboReference
    private UserApi userApi;



    /**
     * 新增活动接口
     * @param activityAddParam
     * @throws BusinessException
     */
    @Override
    public void save(ActivityAddParam activityAddParam) throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        Integer id = userPrinciple.getId();
        AdminDto adminDto=userApi.getAdminById(id);

        ActivityPO poParam = assembleActivityPO(activityAddParam);
        poParam.setBusinessId(adminDto.getBusinessId());
        poParam.setCreateAt(new Date());
        poParam.setRewardType(1);
        poParam.setActivityType(1);
        poParam.setEnableStatus(1);
        activityRepository.save(poParam);

    }


    /**
     * 活动列表
     * @return
     */
    @Override
    public PageResult<ActivityVO> pageList(ActivityAddParam param) {
        // 创建一个新的分页结果对象，用于存储最终的分页查询结果
        PageResult<ActivityVO> voPage = new PageResult<>();
        //total objects pageNo pageSize totalPage
        // 调用 adminRepository 的 gePages 方法进行分页查询，获取包含管理员持久化对象的分页信息
        PageInfo<ActivityPO> pageInfo = activityRepository.pagesList(param);
        //pageNo pageSize total
        // 将分页信息中的页码设置到分页结果对象中
        voPage.setPageNo(pageInfo.getPageNum());
        // 将分页信息中的每页数量设置到分页结果对象中
        voPage.setPageSize(pageInfo.getPageSize());
        // 将分页信息中的总记录数设置到分页结果对象中
        voPage.setTotal(pageInfo.getTotal());
        // 声明一个存储管理员视图对象的列表
        List<ActivityVO> vos = null;
        // 从分页信息中获取管理员持久化对象列表
        List<ActivityPO> pos = pageInfo.getList();
        // 判断持久化对象列表是否不为空
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                ActivityVO vo = new ActivityVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }


    /**
     * 修改活动启动状态
     * @param activityAddParam
     */
    @Override
    public void updateActivityStatus(ActivityAddParam activityAddParam) {
        activityRepository.updateActivityStatus(activityAddParam);

    }

    @Override
    public ActivityVO detail(Integer id) {
        ActivityPO po = activityRepository.detail(id);
        ActivityVO vo = new ActivityVO();
        BeanUtils.copyProperties(po,vo);
        return vo;
    }

    private ActivityPO assembleActivityPO(ActivityAddParam param) {
        ActivityPO po=new ActivityPO();
        BeanUtils.copyProperties(param,po);
        return po;
    }

    @Override
    public void deleteActivity(ActivityAddParam activityAddParam) throws BusinessException {
        activityRepository.deleteActivity(activityAddParam);
    }

    /**
     * 获取各状态活动数量列表
     * @return
     */
    @Override
    public Map<String,Integer> getArticleStatusMap() {
        Map<String,Integer> statusMap = new HashMap<>();
        ArrayList<String> list1 = new ArrayList<>(3);
        list1.add("未开始");
        list1.add("进行中");
        list1.add("已结束");
        for (int i = 0; i < 3; i++) {
            statusMap.put(list1.get(i),activityRepository.getArticleStatusList(i));
        }
        return statusMap;
    }
}
